package com.ks.common.interceptor;

import com.ks.common.enums.BizCodeEnum;
import com.ks.common.model.LoginUser;
import com.ks.common.util.CommonUtil;
import com.ks.common.util.JWTUtil;
import com.ks.common.util.JsonData;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @author KingAusten
 * @date 2022/08/17 17:54:12
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor{

    public static ThreadLocal<LoginUser> threadLocal = new ThreadLocal<>();

    /**
     * @param request
     * @param response
     * @param handler
     *
     * @return
     *
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throws Exception{

        if(HttpMethod.OPTIONS.toString()
                .equalsIgnoreCase(request.getMethod())){
            response.setStatus(HttpStatus.NO_CONTENT.value());
            return true;
        }

        String accessToken = request.getHeader("token");
        if(StringUtils.isBlank(accessToken)){
            accessToken = request.getParameter("token");
        }


        if(StringUtils.isNotBlank(accessToken)){
            Claims claims = JWTUtil.checkJWT(accessToken);
            if(claims == null){
                //未登录
                CommonUtil.sendJsonMessage(response,JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
                return false;
            }

            Long accountNo = Long.parseLong(claims.get("account_no")
                                                    .toString());
            String headImg = (String)claims.get("head_img");
            String username = (String)claims.get("username");
            String mail = (String)claims.get("mail");
            String phone = (String)claims.get("phone");
            String auth = (String)claims.get("auth");

            LoginUser loginUser = LoginUser.builder()
                    .accountNo(accountNo)
                    .auth(auth)
                    .phone(phone)
                    .headImg(headImg)
                    .mail(mail)
                    .username(username)
                    .build();

            //request.setAttribute("loginUser",loginUser);
            //通过threadlocal
            threadLocal.set(loginUser);
            return true;
        }

        CommonUtil.sendJsonMessage(response,JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
        return false;
    }

    /**
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     *
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView) throws Exception{

        HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);
    }

    /**
     * @param request
     * @param response
     * @param handler
     * @param ex
     *
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex) throws Exception{

        threadLocal.remove();
    }

}
